package com.mtons.mblog.base.utils;

import org.simplejavamail.email.Email;
import org.simplejavamail.email.EmailBuilder;
import org.simplejavamail.mailer.Mailer;
import org.simplejavamail.mailer.MailerBuilder;
import org.simplejavamail.mailer.config.TransportStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * try to use simple-java-mail
 */

@Deprecated
public class EmailSender {

    private static final Logger LOGGER = LoggerFactory.getLogger(EmailSender.class);




    private String from;
    private String account;
    private String password;
    private String smtp;

    public String getFrom() {
        return from;
    }

    public String getAccount() {
        return account;
    }

    public String getPassword() {
        return password;
    }

    public String getSmtp() {
        return smtp;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setSmtp(String smtp) {
        this.smtp = smtp;
    }

    public static class REmail{
        private List<String> to;
        private List<String> cc;
        private List<String> bcc;
        private String subject;
        private String content;

        public REmail(EmailBuilder builder) {
            this.to = builder.to;
            this.cc = builder.cc;
            this.bcc = builder.bcc;
            this.subject = builder.subject;
            this.content = builder.content;
        }


        public List<String> getTo() {
            return to;
        }

        public List<String> getCc() {
            return cc;
        }

        public List<String> getBcc() {
            return bcc;
        }

        public String getSubject() {
            return subject;
        }

        public String getContent() {
            return content;
        }

        public static class EmailBuilder {

            private List<String> to;
            private List<String> cc;
            private List<String> bcc;
            private String subject;
            private String content;

            public EmailBuilder(String subject,String content){
                this.subject = subject;
                this.content = content;
            }

            public EmailBuilder to(List<String> to){
                this.to = to;
                return this;
            }

            public EmailBuilder cc(List<String> cc){
                this.cc = cc;
                return this;
            }

            public EmailBuilder bcc(List<String> bcc){
                this.bcc = bcc;
                return this;
            }


            public REmail build(){
                if(CollectionsUtil.isEmpty(to)){
                    throw new IllegalArgumentException("无有效的收件人");
                }
                return new REmail(this);
            }
        }

    }

    public boolean sendMail(REmail email) throws MessagingException {
        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP);
        props.put("mail.smtp.auth", "true");
        Session s = Session.getInstance(props);

        MimeMessage message = new MimeMessage(s);

        InternetAddress from = new InternetAddress(FROM_EMAIL);
        message.setFrom(from);

        List<String> to = email.getTo();

        InternetAddress[] toAddress = new InternetAddress[to.size()];
        for (int i = 0; i < to.size(); i++) {
            toAddress[i] = new InternetAddress(to.get(i));
        }

        message.setRecipients(Message.RecipientType.TO, toAddress);

        List<String> cc = email.getCc();
        if (CollectionsUtil.isNotEmpty(cc)) {
            InternetAddress[] Ccaddress = new InternetAddress[cc.size()];
            for (int i = 0; i < cc.size(); i++) {
                Ccaddress[i] = new InternetAddress(cc.get(i));
            }
            message.setRecipients(Message.RecipientType.CC, Ccaddress);
        }

        List<String> bcc = email.getBcc();
        if (bcc != null) {
            InternetAddress[] Bccaddress = new InternetAddress[bcc.size()];
            for (int i = 0; i < bcc.size(); i++) {
                Bccaddress[i] = new InternetAddress(bcc.get(i));
            }
            message.setRecipients(Message.RecipientType.BCC, Bccaddress);
        }
        message.setSubject(email.getSubject(), "UTF-8");
        message.setSentDate(new Date());

        BodyPart mdp = new MimeBodyPart();
        mdp.setContent(email.getContent(), "text/html;charset=utf-8");
        Multipart mm = new MimeMultipart();
        mm.addBodyPart(mdp);
        message.setContent(mm);

        message.saveChanges();
        Transport transport = s.getTransport("smtp");
        transport.connect(SMTP, (null == ACCOUNT) ? FROM_EMAIL : ACCOUNT, PASS_WORD);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        return true;
    }


    private final static String FROM_EMAIL = "message@chinaredstar.com";
    private final static String ACCOUNT = "group\\message";
    private final static String PASS_WORD = "Redstar123";
    private final static String SMTP = "mail.chinaredstar.com";

//    public boolean sendMail(String subject, String content) {
//        try {
//            String[] toArray = new String[]{"liu.chaobing@chinaredstar.com", "yang.jun536@chinaredstar.com"};
//            return sendMail(toArray, null, null, subject, content);
//        } catch (Exception e) {
//            LOGGER.error("sendMail has exception, the message is: {}", e);
//            return false;
//        }
//    }
    public static void main(String[] args) {
        EmailSender emailSender = new EmailSender();
        emailSender.setAccount(ACCOUNT);
        emailSender.setFrom(FROM_EMAIL);
        emailSender.setPassword(PASS_WORD);
        emailSender.setSmtp(SMTP);
        REmail email = new REmail.EmailBuilder("subject","conent").to(Arrays.asList("ma.hui625@chinaredstar.com")).build();
        try {
            System.out.println(emailSender.sendMail(email));
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        Mailer mailer = MailerBuilder.withSMTPServerHost(SMTP).withSMTPServerPort(25).withTransportStrategy(TransportStrategy.SMTP).withSMTPServerUsername(ACCOUNT).withSMTPServerPassword(PASS_WORD).buildMailer();
        Email email1 = EmailBuilder.startingBlank().from("message",FROM_EMAIL).to("ma.hui625@chinaredstar.com").withSubject("subject").withPlainText("content").buildEmail();
        mailer.sendMail(email1);
    }
}
